<?
    include "../common.php" ;
    $opt = $_POST['opt'] ;
    $arr = [] ;
    if( $opt == 'buyGoods' ){
        $pid = $_POST['pid'] ; 
        $uid = $_POST['resId'] ;
        $num = $_POST['num'] ; 
        $now = Time();
        $sql = "INSERT INTO `carts`(`uId`, `pId`, `pNum`, `shopTime`) VALUES ({$uid},{$pid},{$num},{$now})" ;
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ; 
            $arr['info'] = '购买成功！' ;
            $sql1 = "SELECT * FROM products WHERE id = {$pid}";
            $res1 = mysqli_query($link,$sql1);
            $one = mysqli_fetch_assoc($res1);
            $inventory = $one['inventory'] - $num ; 
            $salesNum = $one['salesNum'] + $num ;
            $sql2 = "UPDATE `products` SET `inventory`={$inventory},`salesNum`={$salesNum} WHERE id = $pid";
            $res2 = mysqli_query($link,$sql2);
        } else {
            $arr['status'] = 0 ; 
            $arr['info'] = '操作失败！' ; 
        }
    }
    echo json_encode($arr);
?>